package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.Commons;
import common.ResultInfo;
import dto.SelectCondition;
import entity.MemOrigin;
import entity.MemType;
import entity.Member;
import net.sf.json.JSONObject;
import service.MemberService;
import service.impl.MemberServiceImpl;
import vo.MemberVO;

@WebServlet("/member/*")
public class MemberServlet extends HttpServlet{

	MemberService service = new MemberServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String requestURI = req.getRequestURI();
		String methodName = requestURI.substring(requestURI.lastIndexOf("/")+1);
		switch(methodName) {
		case "save": save(req,resp); break;
		case "remove": remove(req,resp); break;
		case "update": update(req,resp); break;
		case "getById": getById(req,resp); break;
		case "getByPage": getByPage(req,resp); break;
		case "getByCondition": getByCondition(req,resp); break;
		case "getByPhone": getByPhone(req,resp); break;
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}

	public void getByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.得到参数
		String page = req.getParameter("page");
		int pg = page == null? 1 : Integer.parseInt(page);
		//2.调用服务分页
		List<MemberVO> list = service.getByPage(Commons.PAGE_SIZE,pg);
		//调用查询分页的代码
		int totalCount = service.getTotalCount();
		
		//3.判断集合是空集合就查询失败
		ResultInfo result = new ResultInfo();
		if (!list.isEmpty()) {
			result.setCode(200);
			result.setMessage("查询成功");
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("members", list);
			map.put("totalCount", totalCount);
			result.setData(map);
		}else {
			result.setCode(10001);
			result.setMessage("查询失败");
		}
		//4.得到输出流
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}

	public void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.得到参数id
		String memberId = req.getParameter("memberId");
		//2.服务根据id得到Member
		MemberVO member = service.getById(Integer.parseInt(memberId));
		//3创建统一结果集
		ResultInfo result = new ResultInfo();
		
		if (member != null) {
			result.setCode(200);
			result.setMessage("查询成功");
			List<MemberVO> list = new ArrayList<>();
			list.add(member);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("members", list);
			map.put("totalCount", 1);
			result.setData(map);
		}else {
			result.setCode(10001);
			result.setMessage("查询失败");
		}
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void getByCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.得到参数
		String type = req.getParameter("type");
		String origin = req.getParameter("origin");
		String stat = req.getParameter("stat");
		String page = req.getParameter("page");
		//2.封装
		int pg = page == null? 1 :Integer.parseInt(page);
		SelectCondition cond = new SelectCondition(type, origin, stat, pg);
		//3.调用方法
		List<MemberVO> list = service.getByCondition(cond);
		//调用分页
		int totalCount = service.getCountByCondition(cond);
		
		ResultInfo result = new ResultInfo();
		if (!list.isEmpty()) {
			result.setCode(200);
			result.setMessage("查询成功");
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("members", list);
			map.put("totalCount", totalCount);
			result.setData(map);
		}else {
			result.setCode(10001);
			result.setMessage("查询失败");
		}
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void getByPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.得到参数id
		String phone = req.getParameter("phone");
		//2.服务根据id得到Member
		MemberVO member = service.getByPhone(phone);
		//3创建统一结果集
		ResultInfo result = new ResultInfo();
		if (member != null) {
			result.setCode(200);
			result.setMessage("查询成功");
			List<MemberVO> list = new ArrayList<>();
			list.add(member);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("members", list);
			map.put("totalCount", 1);
			result.setData(map);
		}else {
			result.setCode(10001);
			result.setMessage("查询失败");
		}

		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取参数
		String memberNick = req.getParameter("memberNick");
		String memberPhone = req.getParameter("memberPhone");
		String originId = req.getParameter("originId");
		// 2.封装
		Member member = new Member();
		member.setMemberNick(memberNick);
		member.setMemberPhone(memberPhone);
		MemOrigin origin = new MemOrigin();
		origin.setOriginId(Integer.parseInt(originId));
		member.setOrigin(origin);
		// 3.调用服务
		boolean res = service.save(member);
		ResultInfo result = new ResultInfo();
		if (res) {
			result.setCode(200);
			result.setMessage("添加成功");
		}else {
			result.setCode(20001);
			result.setMessage("添加失败");
		}
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取参数一堆
		String[] ids = req.getParameterValues("memberId");
		// 2.循环处理到一个集合中
		List<Integer> idList = new ArrayList<>();
		// 把数组变成Steam<String>,map处理成Stream<Integer>
		Stream.of(ids).map(id->Integer.parseInt(id)).forEach(idList::add);
		
		// 3.调用服务
		boolean res = service.removeByIds(idList);
		
		ResultInfo result = new ResultInfo();
		if (res) {
			result.setCode(200);
			result.setMessage("更新成功");
		}else {
			result.setCode(20001);
			result.setMessage("更新失败");
		}
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取参数
		String memberId = req.getParameter("memberId");
		String memberNick = req.getParameter("memberNick");
		String memberPhone = req.getParameter("memberPhone");
		String money = req.getParameter("money");
		String typeId = req.getParameter("typeId");
		String stat = req.getParameter("stat");
		
		// 2.封装
		Member member = new Member();
		member.setMemberId(Integer.parseInt(memberId));
		member.setMemberNick(memberNick);
		member.setMemberPhone(memberPhone);
		member.setMoney(Double.parseDouble(money));
		int st = stat == null ? 0 : Integer.parseInt(stat);
		member.setStat(st);
		MemType type = new MemType();
		type.setTypeId(Integer.parseInt(typeId));
		member.setType(type);
		
		// 3.调用服务
		boolean res = service.update(member);
		ResultInfo result = new ResultInfo();
		if (res) {
			result.setCode(200);
			result.setMessage("更新成功");
		}else {
			result.setCode(20001);
			result.setMessage("更新失败");
		}
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
}
